package com.kuang.gulimall.cart.vo;

import java.math.BigDecimal;
import java.util.List;

/*
整个购物车内容
购物车内容都要计算,重写get方法，保证每次获取属性都会计算
* */
public class CartVo {

    List<CartItemVo> items;
    private Integer countNum;
    private Integer countType;
    private BigDecimal totalAmount;
    private BigDecimal reduce = new BigDecimal("0.00");

    public List<CartItemVo> getItems() {
        return items;
    }

    public void setItems(List<CartItemVo> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        //如果数不为0的情况，那么总数等于各个的和
        int count = 0;
        if(items!=null&&items.size()>0){
            for (CartItemVo item : items) {
                count += item.getCount();
            }
        }
        return count;
    }

    public void setCountNum(Integer countNum) {
        this.countNum = countNum;
    }

    public Integer getCountType() {
        int count = 0;
        if (items!=null&&items.size()>0){
            for (CartItemVo item : items) {
                count+=1;
            }
        }
        return count;
    }

    public void setCountType(Integer countType) {
        this.countType = countType;
    }

    public BigDecimal getTotalAmount() {
        //1.计算购物项总价
        BigDecimal amount = new BigDecimal("0");
        if (items!=null&&items.size()>0){
            for (CartItemVo item : items) {
                if (item.getCheck()){
                    BigDecimal totalPrice = item.getTotalPrice();
                    amount = amount.add(totalPrice);
                }
            }
        }

        //2，减去优惠总价
        BigDecimal subtract = amount.subtract(getReduce());
        return subtract;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
